import { _decorator, AudioSource, Component, AnimationClip, Animation, Color, find, Node, tween, Vec3, Tween, AudioClip, UITransform, Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Door')
export class Door extends Component {
    @property({type: Node})
    enterButtonNode: Node = null!

    @property({type: AudioClip})
    openDoorAudio: AudioClip = null!

    @property({type: AudioClip})
    closeDoorAudio: AudioClip = null!

    enterButtonTween: Tween<Node> = null
    isOpenDoorAnimPlayed: boolean = false
    isCloseDoorAnimPlayed: boolean = false

    start() {
        // 渐显
        this.setAlphaToZero()
        this.schedule(this.show, 0.08)
    }

    /** 
     * @zh
     * 逐渐变得不透明。
    */
    show() {
        let color = this.node.getComponent(Sprite).color
        let newAlphaValue = color.a + 5
        this.node.getComponent(Sprite).color = new Color(color.r, color.g, color.b, newAlphaValue)
        
        if (newAlphaValue >= 255) {
            this.unschedule(this.show)
        }
    }
    
    /** 
     * @zh
     * 一开始要设置图片的alpha值为0，否则刚生成像素块时设置的十六进制颜色就会直接让计时器中的逻辑无效。
    */
    setAlphaToZero() {
        let color = this.node.getComponent(Sprite).color
        this.node.getComponent(Sprite).color = new Color(color.r, color.g, color.b, 0)
    }

    /** 
     * @zh
     * 玩家靠近门时会出现挑战Boss或者撤离按钮。
    */
    showButtonWhenPlayerCloseToTheDoor() {
        // 如果玩家靠近了门
        if (this.node.getComponent(UITransform).getBoundingBox().intersects(find('Canvas/Player').getComponent(UITransform).getBoundingBox())) {
            // 播放开门动画
            if (!this.isOpenDoorAnimPlayed) {
                find('Canvas').getComponent(AudioSource).playOneShot(this.openDoorAudio)
                this.node.getComponent(Animation).getState('door').wrapMode = AnimationClip.WrapMode.Normal
                this.node.getComponent(Animation).play('door')

                this.isOpenDoorAnimPlayed = true
                this.isCloseDoorAnimPlayed = false
            }

            // 显示按钮节点
            this.enterButtonNode.active = true

            // 按钮变大变小缓动
            if (!this.enterButtonTween) {
                this.enterButtonTween = tween(this.enterButtonNode).to(0.4, {'scale': new Vec3(1.2, 1.2, 1)})
                                                                   .to(0.4, {'scale': new Vec3(1, 1, 1)})
                                                                   .union()
                                                                   .repeatForever()
                                                                   .start()
            }
            
        }

        // 如果玩家远离了门
        else {
            this.enterButtonNode.active = false
            this.enterButtonTween = null

            // 播放关门动画和音效
            if (!this.isCloseDoorAnimPlayed && this.isOpenDoorAnimPlayed) {
                find('Canvas').getComponent(AudioSource).playOneShot(this.closeDoorAudio)
                this.node.getComponent(Animation).getState('door').wrapMode = AnimationClip.WrapMode.Reverse
                this.node.getComponent(Animation).play('door')

                this.isCloseDoorAnimPlayed = true
                this.isOpenDoorAnimPlayed = false
            }
        }
    }
    
    update(deltaTime: number) {
        this.showButtonWhenPlayerCloseToTheDoor()
    }
}


